
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;

/**
 * Created by Scott Tiger.
 * User: tomaszsocha
 * Date: 2010-03-29
 * Time: 14:08:38
 */
public class Zbrojownia {
    public static HashMap<String,Object> laczenia= new HashMap<String,Object>();
    public static String filtrpref;
    public static String filtrsuf;
    public static boolean jest;
    public static int deep;
    public static HashMap <String,Koszulka> zbior = new HashMap<String,Koszulka>();
    public static HashMap <String,Koszulka> zbiorB = new HashMap<String,Koszulka>();
    public static HashMap <String,Object> zbiorC = new HashMap<String,Object>();
    public static Collection<Koszulka> fajne = new LinkedHashSet();
    public static HashMap <String,Integer> wagi = new HashMap<String,Integer>();
    public static Collection<Koszulka> zbrojownia= new LinkedList();
    public static JTextField licznikZK;
    public static JTextField liczniczek;
    public static JPanel ostatnia;
    public static JPanel jpanel_0;
    public static long couter;

    public static void main(String[] args) {
        String zbrojownia_name  ="/Users/tomaszsocha/Desktop/bw/";
        String f_pre_name ="/Users/tomaszsocha/Desktop/bw/";
        String f_suf_name ="/Users/tomaszsocha/Desktop/bw/";
        if (args.length >0 ) {
           zbrojownia_name  +=args[0];
        } else {
            zbrojownia_name  +="zk_mniejsza.csv";
        }
        if (args.length > 1) {
            f_pre_name  +=args[1];
        } else {
            f_pre_name  +="filtrPref.csv";
        }
        if (args.length > 2) {
            f_suf_name  +=args[2];
        } else {
            f_suf_name  +="filtrSuf.csv";
        }

        File zkFile = new File(zbrojownia_name);

        //HashMap<String,Object> laczenia= new HashMap<String,Object>();
        File prefixyFile = new File("/Users/tomaszsocha/Desktop/bw/prefy.csv");
        File sufixyFile = new File("/Users/tomaszsocha/Desktop/bw/sufy.csv");
        File filtrPFile = new File(f_pre_name);
        File filtrSFile = new File(f_suf_name);
        File wagaP = new      File("/Users/tomaszsocha/Desktop/bw/wagaPref.csv");
        File wagaS = new File("/Users/tomaszsocha/Desktop/bw/wagaSuf.csv");

      Action action = new AbstractAction("Button Label") {
      // This method is called when the button is pressed
      public void actionPerformed(ActionEvent evt) {
        // Perform action...
      }
        };

       JButton button = new JButton(action);
       ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("I was selected.");
      }
    };



        try {
         initWagi(wagaP,wagaS);
         filtrpref = initfiltr(filtrPFile);
         filtrsuf  = initfiltr(filtrSFile);
         laczenia =    processLineByLine(prefixyFile,laczenia);
         laczenia =    processLineByLine(sufixyFile,laczenia);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            zbrojownia = initZbrojownia(zkFile,zbrojownia,wagi);
            deep = zbrojownia.size()-23;
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


      jpanel_0 = new JPanel(new GridLayout(0,1));
      JScrollPane scrollpanel = new JScrollPane(jpanel_0);

      jpanel_0.setBackground(Color.pink);
      //for(int i=0;i<3;++i) jpanel_0.add(new JLabel("label"+i));

      JPanel jpanel_1 = new JPanel(new GridLayout(0,1));
      jpanel_1.setBackground(Color.cyan);
      JList zk = new JList();

       Iterator zit  = zbrojownia.iterator();
       JComboBox combo = new JComboBox();
        while (zit.hasNext()){
            Koszulka key = (Koszulka) zit.next();
            JFormattedTextField textOpis = new JFormattedTextField();

            KoszulkaGUI nastepna = new KoszulkaGUI(key);

            JPanel koszuleczka = new JPanel();
            JTextField textPref = new JTextField();
            JTextField textSuf = new JTextField();
            textOpis.setText(key.getOpis());
            textPref.setText(key.getPrefix());
            textSuf.setText(key.getSufix());
            koszuleczka.add(textOpis);
            koszuleczka.add(textPref);
            koszuleczka.add(textSuf);
            //jpanel_0.add(koszuleczka);
            //zk.add(koszuleczka);
            //ostatnia = koszuleczka;

            //jpanel_0.add(nastepna);
            combo.addItem(nastepna.getItself().getPrefix()+" "+nastepna.getItself().getSufix());


       }

     // jpanel_0.add(ostatnia);
     // jpanel_0.add(button);
      //jpanel_0.add(zk);
        jpanel_0.add(combo);
      licznikZK = new JTextField(zbrojownia.size());
      liczniczek = new JTextField();
      jpanel_1.add(licznikZK);
      jpanel_1.add(liczniczek);
      JFrame frame = new JFrame();
      Container cp = frame.getContentPane();
      cp.add(scrollpanel,BorderLayout.CENTER);
      //cp.add(jpanel_0,BorderLayout.WEST);
      cp.add(jpanel_1,BorderLayout.EAST);

      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);






      /*
        Iterator it ;
         Koszulka k ;
         Koszulka k2 ;
        String key;// = it.next().toString();


        it = zbrojownia.iterator();
          */
        System.out.println("deep: "+(zbrojownia.size() - deep));
        System.out.println("zbrojownia: "+zbrojownia.size());

        polaczKoszulke(zbrojownia);

        Iterator it  = zbior.keySet().iterator();
        while (it.hasNext()){
            String key = (String)it.next();
            System.out.println(key+""+zbior.get(key));
        }



/*



        for (int i=0;i<zbrojownia.size();i++){
            Koszulka shirt = (Koszulka) zbrojownia.toArray()[i];
            zbrojownia.remove(shirt);
            System.out.println(shirt.getPrefix()+"  "+shirt.getSufix());
            Calendar calendar = new GregorianCalendar();
                String am_pm;
                int hour = calendar.get(Calendar.HOUR);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
                if(calendar.get(Calendar.AM_PM) == 0)
                  am_pm = "AM";
                else
                  am_pm = "PM";
                System.out.println("Current Time : " + hour + ":"
                + minute + ":" + second + " " + am_pm);

            polaczKoszulke(zbrojownia,shirt);
            zbrojownia.add(shirt);
           }
 */
/*
        while (it.hasNext()) {

            //key = it.next().toString();
            zbrojownia.size();
            k1 = (Koszulka) it.next();


            if (it.hasNext()){
            k2 = (Koszulka) it.next();


            Koszulka wynik = polaczKoszulki(k1,k2);
            System.out.println(k1.getPrefix()+"  "+k1.getSufix()+" + "+k2.getPrefix()+"  "+k2.getSufix());
            System.out.println(wynik.getPrefix()+"  "+wynik.getSufix());
            zbrojownia.add(wynik);            bw
            zbrojownia.remove(k2);
            }
            zbrojownia.remove(k1);

            it = zbrojownia.iterator();
        }
*/
    }

    private static void initWagi(File wagaP, File wagaS) throws FileNotFoundException {
       Scanner scanner = new Scanner(wagaP,"UTF-8");
        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
              String line = scanner.nextLine() ;
              String name = line.substring(0, line.indexOf(","));
              int waga = (int) Integer.parseInt(line.substring(line.indexOf(",")+1));
              wagi.put(name,waga);
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
        scanner = new Scanner(wagaS,"UTF-8");
        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
              String line = scanner.nextLine() ;
              String name = line.substring(0, line.indexOf(","));
              int waga = (int) Integer.parseInt(line.substring(line.indexOf(",")+1));
              wagi.put(name,waga);
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
    }


    public static final HashMap processLineByLine(File fFile,HashMap laczenia) throws FileNotFoundException {
    Scanner scanner = new Scanner(fFile,"UTF-8");
    try {
      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
          String line = scanner.nextLine() ;
          String result = line.substring(0, line.indexOf(","));
          String param2 = line.substring(line.indexOf(",")+1,line.indexOf(",",line.indexOf(",")+1));
          String param1 = line.substring(line.indexOf(",",line.indexOf(",")+1)+1);
          HashMap<String,String> o = new HashMap<String,String>();
          if (laczenia.containsKey(param1)) {
          o = (HashMap<String,String>) laczenia.get(param1);}
          o.put(param2,result);
          laczenia.put(param1,o);



      }
    }
    finally {
      //ensure the underlying stream is always closed
      scanner.close();
    }
        return laczenia;
  }




    public static HashMap initLaczenia(HashMap laczenia){
        String param1 = "";
        String param2 = "";
        String result = "";
        HashMap<String,String> o = new HashMap<String,String>();
        if (laczenia.containsKey("")) {
        o = (HashMap<String,String>) laczenia.get(param1);}
        o.put(param2,result);
        laczenia.put(param1,o);
        return o;

    };





    public static Collection initZbrojownia(File fFile,Collection z, HashMap<String,Integer> wagi) throws FileNotFoundException {
    Scanner scanner = new Scanner(fFile,"UTF-8");
    try {
      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
          String line = scanner.nextLine() ;
          String pref = line.substring(0, line.indexOf(","));
          String suf =line.substring(line.indexOf(",")+1);
          z.add(new Koszulka(pref,suf,wagi));
      }

      Iterator it = z.iterator();
        while (it.hasNext()){
            System.out.println(((Koszulka) it.next()).getIngredients());
        }
    }
    finally {
      //ensure the underlying stream is always closed
      scanner.close();
    }


        return z;
    }

    public static String initfiltr(File fFile) throws FileNotFoundException {
    Scanner scanner = new Scanner(fFile,"UTF-8");
      String line = "";
        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
              if (line.equals("")){
                  line = scanner.nextLine() ;
              }   else {
                  line += "|"+scanner.nextLine();
              }
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
      return line;
    }



    public void polacz(HashMap laczenia,Collection zbrojownia){
        Koszulka k1;
        Koszulka k2;

    }


    public Collection<Koszulka> getZbrojownia(){
        return zbrojownia;
    }

    public static String polacz(String pref1,String pref2){
       return (String) ((HashMap) laczenia.get(pref1)).get(pref2);

    }

    public static Koszulka polaczKoszulki(Koszulka k1,Koszulka k2){
         k1.getPrefix();
         k2.getPrefix();
//        System.out.println(k1.getPrefix()+"  "+k1.getSufix()+" + "+k2.getPrefix()+"  "+k2.getSufix() + "polacz");
         couter ++;
         Koszulka wynik = new  Koszulka(polacz(k1.getPrefix(),k2.getPrefix()),polacz(k1.getSufix(),k2.getSufix()),k1,k2,wagi);
         return wynik;
    }


    public static void polaczKoszulke(Collection zk,Koszulka k ) {
        if (zk.size() > 0) {
        for (int i =0;i<zk.size();i++ ) {
        Koszulka shirt = (Koszulka) zk.toArray()[i];
        zk.remove(shirt);
        Koszulka newShirt = polaczKoszulki(k,shirt);

            if (((newShirt.getPrefix().matches(filtrpref) ) && (newShirt.getSufix().matches(filtrsuf)) || (newShirt.getSufix().matches(filtrsuf) )))
            {
                System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());
                System.out.println(newShirt.getPrefix()+"  "+newShirt.getSufix());
                jest = true;
            }
        if (zk.size()>0) {
            polaczKoszulke(zk,newShirt);
        }

        zk.add(shirt);


            }
        }
    }

        public static void polaczKoszulke(Collection zk) {
            licznikZK.setText(Integer.toString(zk.size()));
        if (zk.size() > deep ) {
            for (int i =0;i<zk.size()-1;i++ ) {
            Koszulka shirt = (Koszulka) zk.toArray()[i];
            if (zk.size()>22) {


            Calendar calendar = new GregorianCalendar();
                String am_pm;
                int hour = calendar.get(Calendar.HOUR);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
                if(calendar.get(Calendar.AM_PM) == 0)
                  am_pm = "AM";
                else
                  am_pm = "PM";
              // System.out.println("Current Time : " + hour + ":"
              //  + minute + ":" + second + " " + am_pm+ " size :"+zk.size()+" -- "+shirt.getPrefix()+"  "+shirt.getSufix()) ;
            }

            zk.remove(shirt);
                for (int j =0;j<zk.size()-1;j++ ) {
                Koszulka k = (Koszulka) zk.toArray()[j];
                zk.remove(k);
                Koszulka newShirt = polaczKoszulki(k,shirt);
                  if (czyWarto(shirt,k,newShirt)) {
                   // System.out.println(newShirt.getPrefix()+"  "+newShirt.getSufix());
                   // System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());

                    if (((newShirt.getPrefix().matches(filtrpref) ) && (newShirt.getSufix().matches(filtrsuf)) ))//|| (newShirt.getSufix().matches(filtrsuf) )))
                    {
                       // System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());
                        //System.out.println(newShirt.getPrefix()+"  "+newShirt.getSufix());

                        if (!zbior.containsKey(newShirt.getPrefix()+newShirt.getSufix()+k.getPrefix()+k.getSufix()+shirt.getPrefix()+shirt.getSufix())) {


                        zbior.put(newShirt.getPrefix()+newShirt.getSufix()+k.getPrefix()+k.getSufix()+shirt.getPrefix()+shirt.getSufix(),newShirt);
                        zbiorB.put(newShirt.getPrefix()+"."+newShirt.getSufix(),newShirt);
                        Collection<Koszulka> kList = new LinkedList();
                        if (zbiorC.containsKey(newShirt.getPrefix()+"."+newShirt.getSufix())){
                           kList = (LinkedList)zbiorC.get(newShirt.getPrefix()+"."+newShirt.getSufix());
                           kList.add(newShirt);
                        } else {
                           kList.add(newShirt);
                           System.out.println(newShirt.getPrefix()+"  "+newShirt.getSufix()+" cost :" + newShirt.totalCost());
                           System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());
                           System.out.println(newShirt.getIngredients());
                           time();
                           System.out.println(zbiorC.size());

                           JPanel koszuleczka = new JPanel();
                                JTextField textPref = new JTextField();
                                JTextField textSuf = new JTextField();

                                textPref.setText(newShirt.getPrefix());
                                textSuf.setText(newShirt.getSufix());

                                koszuleczka.add(textPref);
                                koszuleczka.add(textSuf);
                                jpanel_0.add(koszuleczka);
                                liczniczek.setText(Integer.toString(zbiorC.size()));

                        }

                        zbiorC.put(newShirt.getPrefix()+"."+newShirt.getSufix(),kList);
                        fajne.add(newShirt);
                        jest = true;

                        }
                    }

                    zk.add(newShirt);
                   if (zk.size()>1)
                    polaczKoszulke(zk);
                    zk.remove(newShirt);
                  }
                zk.add(k);


                 }
            zk.add(shirt);

            }
        }
    }

    private static void time() {
        Calendar calendar = new GregorianCalendar();
            String am_pm;
            int hour = calendar.get(Calendar.HOUR);
            int minute = calendar.get(Calendar.MINUTE);
            int second = calendar.get(Calendar.SECOND);
            if(calendar.get(Calendar.AM_PM) == 0)
              am_pm = "AM";
            else
              am_pm = "PM";
           System.out.println("Current Time : " + hour + ":"
            + minute + ":" + second + " " + am_pm) ;
   }


    private static boolean czyWarto(Koszulka shirt, Koszulka k, Koszulka newShirt) {

           if (shirt.getWagaPref()==newShirt.getWagaPref() && shirt.getWagaSuf() >= newShirt.getWagaSuf() ) {
                return false;
            } else if (k.getWagaPref()==newShirt.getWagaPref() && k.getWagaSuf() >= newShirt.getWagaSuf() ) {
                return false;
            } else if (shirt.getWagaPref()>=newShirt.getWagaPref() && shirt.getWagaSuf() == newShirt.getWagaSuf() ) {
                return false;
            } else if (k.getWagaPref()>=newShirt.getWagaPref() && k.getWagaSuf() == newShirt.getWagaSuf() ) {
                return false;

            } else  if (k.getWaga()>newShirt.getWaga() || shirt.getWaga() > newShirt.getWaga() ) {
                return false;

            }

            else

            { return (!(newShirt.czySkladnik(newShirt))); }


    }

}
